package main

import (
	"fmt"
	"log"
	"net"
)

func main() {
	localIP := net.ParseIP("127.0.0.1")
	localAddr := &net.UDPAddr{
		IP:   localIP,
		Port: 2000,
	}
	remoteAddr := &net.UDPAddr{
		IP:   localIP,
		Port: 3000,
	}
	udp, err := net.DialUDP("udp", localAddr, remoteAddr)
	if err != nil {
		log.Fatal(err)
		return
	}
	defer udp.Close()

	data := make([]byte, 1024)
	for {
		n, err := udp.Read(data)
		if err != nil {
			fmt.Printf("接受消息错误，%v\n", err)
			continue
		}
		fmt.Println("接受消息:", string(data[:n]))

		write, err := udp.Write([]byte("ok"))
		if err != nil {
			fmt.Printf("发送消息错误，%v\n", err)
			continue
		}
		fmt.Println("发送消息长度:", write)
	}
}
